from flask_login import current_user
from flask_restful import Resource, marshal_with,reqparse

from core.roles.role_check import check_perms
from libs.helper import StrBoolNone
from libs.login import login_required
from controllers.console import api
from fields.online_mall.prod_category_fields_def import base_category_fields,category_list_fields
from services.online_mall.pick_addr_service import PickAddrService
from services.online_mall.prod_category_service import ProdCategoryService

class ProdCategoryApi(Resource):
    @login_required
    @marshal_with(category_list_fields)
    @check_perms(perms=["prod:category:list"])
    def get(self):
        """
        获取商品分类列表
        """
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=False,location='args')
        parser.add_argument("shop_id",type=str,required=False,location='args')
        parser.add_argument("parent_id",type=str,required=False,location='args')
        parser.add_argument("name",type=str,required=False,location='args')
        parser.add_argument("status",StrBoolNone,required=False,location='args')
        parser.add_argument("page",type=int,required=False,default=-1,location='args')
        parser.add_argument("page_size",type=int,required=False,default=10,location='args')
        args = parser.parse_args()
        return ProdCategoryService.get_category_list(current_user,args)
    
    @login_required
    @marshal_with(base_category_fields)
    @check_perms(perms=["prod:category:create"])
    def post(self):
        """
        获取商品分类列表
        """
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=False,location='json')
        parser.add_argument("shop_id",type=str,required=True,location='json')
        parser.add_argument("parent_id",type=str,required=False,location='json')
        parser.add_argument("name",type=str,required=True,location='json')
        parser.add_argument("icon_file_id",type=str,required=False,location='json')
        parser.add_argument("pic_file_id",type=str,required=False,location='json')
        parser.add_argument("seq",type=int,required=False,location='json')
        parser.add_argument("status",StrBoolNone,required=False,location='json')
        
        args = parser.parse_args()
        return ProdCategoryService.create_category(current_user,args)
    
    @login_required
    @marshal_with(base_category_fields)
    @check_perms(perms=["prod:category:update"])
    def put(self):
        """
        获取商品分类列表
        """
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=False,location='json')
        parser.add_argument("shop_id",type=str,required=True,location='json')
        parser.add_argument("parent_id",type=str,required=False,location='json')
        parser.add_argument("name",type=str,required=True,location='json')
        parser.add_argument("icon_file_id",type=str,required=False,location='json')
        parser.add_argument("pic_file_id",type=str,required=False,location='json')
        parser.add_argument("seq",type=int,required=False,location='json')
        parser.add_argument("status",StrBoolNone,required=False,location='json')
        
        args = parser.parse_args()
        return ProdCategoryService.update_category(current_user,args)
    
    @login_required
    @marshal_with(base_category_fields)
    @check_perms(perms=["prod:category:del"])
    def delete(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=False,location='json')
        args = parser.parse_args()
        return ProdCategoryService.delete_category(current_user,args)
    
api.add_resource(ProdCategoryApi, '/prod/category')